﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Friso.Entities;
using Friso.Business;
using FetCo.Data;
using FetCo.Framework;
using System.IO;
public partial class V_FactoryManagement_Building : System.Web.UI.Page
{

    #region viewstate
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    static string prevPage = String.Empty;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["CurrentMenu"] = (int)Menus.Article;
        Session["SubCurrentMenu"] = (int)SubMenus.ListArticle;
        if (IsPostBack)
            return;
        if (Request.UrlReferrer != null)
            prevPage = Request.UrlReferrer.ToString();
        else
            prevPage = "Default.aspx";
        if (IsPostBack)
        {
            return;
        }
        if (UserRight.GetUserPermission() != FetConstant.Admin)
        {
            Response.Redirect("~/Login.aspx");
        }
        else
        {
            string mes = Request.QueryString["alert"];
            if (!string.IsNullOrEmpty(mes))
            {
                // ltrmessage.Text = mes;
            }
            btnDeleteImg.Visible = false;
            img.Visible = false;
            LoadDDlCategory();
            LoadData();

        }
    }
    protected void LoadDDlCategory()
    {
        Criteria cri = new Criteria();
        ddlCategory.Items.Clear();
        ddlCategory.Items.Add(new ListItem((string)Utilitys.Dictionary["option"], "0"));
        CategoryCollection _CategoryCollection = CategoryService.GetCategoryListByParam("[" + Category._Table + "].[" + CategoryColumns.Name + "]", OrderDirection.ASC, cri.Criter);
        foreach (Category _Category in _CategoryCollection)
        {
            ddlCategory.Items.Add(new ListItem(_Category.Name, _Category.CategoryID.ToString()));
        }
        LoadDDlSubCategory(long.Parse(ddlCategory.SelectedValue));
    }
    protected void LoadDDlCategory_where_catid(long id)
    {
        Criteria cri = new Criteria();
        ddlCategory.Items.Clear();
        ddlCategory.Items.Add(new ListItem((string)Utilitys.Dictionary["option"], "0"));
        cri.add("[" + Category._Table + "].[" + CategoryColumns.CategoryID + "]", id, Condition.EQUAL, Condition.AND);
        CategoryCollection _CategoryCollection = CategoryService.GetCategoryListByParam("[" + Category._Table + "].[" + CategoryColumns.Name + "]", OrderDirection.ASC, cri.Criter);
        foreach (Category _Category in _CategoryCollection)
        {
            ddlCategory.Items.Add(new ListItem(_Category.Name, _Category.CategoryID.ToString()));
        }
    }
    protected void LoadDDlSubCategory(long CategoryID)
    {
        Criteria cri = new Criteria();
        cri.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", CategoryID, Condition.EQUAL, Condition.AND);
        cri.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", 0, Condition.EQUAL, Condition.AND);
        SubCategoryCollection _SubCategoryCollection = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.Name + "]", OrderDirection.ASC, cri.Criter);
        SubCategory _SubCategory = new SubCategory();
        ddlSubCat.Items.Clear();
        ddlSubCat.Items.Add(new ListItem((string)Utilitys.Dictionary["option"], "0"));
        if (_SubCategoryCollection.Count > 0)
        {
            for (int i = 0; i < _SubCategoryCollection.Count; i++)
            {
                _SubCategory = _SubCategoryCollection[i];
                string l1 = count_(_SubCategory.SubcategoryLevel);
                ddlSubCat.Items.Add(new ListItem(l1 + _SubCategory.Name + l1, _SubCategory.SubCategoryID.ToString()));

                Criteria cri1 = new Criteria();
                cri1.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", CategoryID, Condition.EQUAL, Condition.AND);
                cri1.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", _SubCategory.SubCategoryID, Condition.EQUAL, Condition.AND);
                SubCategoryCollection _SubCategoryCollection1 = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.Name + "]", OrderDirection.ASC, cri1.Criter);
                SubCategory _SubCategory1 = new SubCategory();
                if (_SubCategoryCollection1.Count > 0)
                {
                    for (int i1 = 0; i1 < _SubCategoryCollection1.Count; i1++)
                    {
                        _SubCategory1 = _SubCategoryCollection1[i1];
                        string l2 = count_(_SubCategory1.SubcategoryLevel);
                        ddlSubCat.Items.Add(new ListItem(l2 + _SubCategory1.Name + l2, _SubCategory1.SubCategoryID.ToString()));

                        Criteria cri2 = new Criteria();
                        cri2.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", CategoryID, Condition.EQUAL, Condition.AND);
                        cri2.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", _SubCategory1.SubCategoryID, Condition.EQUAL, Condition.AND);
                        SubCategoryCollection _SubCategoryCollection2 = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.Name + "]", OrderDirection.ASC, cri2.Criter);
                        SubCategory _SubCategory2 = new SubCategory();
                        if (_SubCategoryCollection2.Count > 0)
                        {
                            for (int i2 = 0; i2 < _SubCategoryCollection2.Count; i2++)
                            {
                                _SubCategory2 = _SubCategoryCollection2[i2];
                                string l3 = count_(_SubCategory2.SubcategoryLevel);
                                ddlSubCat.Items.Add(new ListItem(l3 + _SubCategory2.Name + l3, _SubCategory2.SubCategoryID.ToString()));

                                Criteria cri3 = new Criteria();
                                cri3.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", CategoryID, Condition.EQUAL, Condition.AND);
                                cri3.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", _SubCategory2.SubCategoryID, Condition.EQUAL, Condition.AND);
                                SubCategoryCollection _SubCategoryCollection3 = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.Name + "]", OrderDirection.ASC, cri3.Criter);
                                SubCategory _SubCategory3 = new SubCategory();
                                if (_SubCategoryCollection3.Count > 0)
                                {
                                    for (int i3 = 0; i3 < _SubCategoryCollection3.Count; i3++)
                                    {
                                        _SubCategory3 = _SubCategoryCollection3[i3];
                                        string l4 = count_(_SubCategory3.SubcategoryLevel);
                                        ddlSubCat.Items.Add(new ListItem(l4 + _SubCategory3.Name + l4, _SubCategory3.SubCategoryID.ToString()));

                                        Criteria cri4 = new Criteria();
                                        cri4.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", CategoryID, Condition.EQUAL, Condition.AND);
                                        cri4.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", _SubCategory3.SubCategoryID, Condition.EQUAL, Condition.AND);
                                        SubCategoryCollection _SubCategoryCollection4 = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.Name + "]", OrderDirection.ASC, cri4.Criter);
                                        SubCategory _SubCategory4 = new SubCategory();
                                        if (_SubCategoryCollection4.Count > 0)
                                        {
                                            for (int i4 = 0; i4 < _SubCategoryCollection4.Count; i4++)
                                            {
                                                _SubCategory4 = _SubCategoryCollection4[i4];
                                                string l5 = count_(_SubCategory4.SubcategoryLevel);
                                                ddlSubCat.Items.Add(new ListItem(l5 + _SubCategory4.Name + l5, _SubCategory4.SubCategoryID.ToString()));
                                            }

                                        }
                                    }

                                }
                            }
                        }

                    }
                }
            }
        }
    }
    private string count_(int number)
    {
        string ck = string.Empty;
        if (number == 0)
        {
            ck = "-";
        }
        else if (number == 1)
        {
            ck = "--";
        }
        else if (number == 2)
        {
            ck = "---";
        }
        else if (number == 3)
        {
            ck = "----";
        }
        else if (number == 4)
        {
            ck = "-----";
        }
        else
        {
            ck = "------";
        }
        return ck;
    }
    protected void LoadData()
    {
        string strid = Request.QueryString["id"];

        if (!string.IsNullOrEmpty(strid))
        {
            this.Id = long.Parse(strid);
            Article _Article = ArticleService.GetArticle(this.Id);
            if (_Article != null)
            {
                txtName.Text = _Article.Title;
                Criteria cri = new Criteria();
                cri.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.SubCategoryID + "]", _Article.SubCatId, Condition.EQUAL, Condition.AND);
                SubCategoryCollection _SubCategoryCollection = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.Name + "]", OrderDirection.ASC, cri.Criter);
                SubCategory _SubCategory = new SubCategory();
                if (_SubCategoryCollection.Count > 0)
                {
                    _SubCategory = _SubCategoryCollection[0];
                }
                LoadDDlCategory();
                ddlCategory.SelectedValue = _SubCategory.CategoryID.ToString();
                LoadDDlSubCategory(_SubCategory.CategoryID);
                ddlSubCat.SelectedValue = _Article.SubCatId.ToString();
                fck_SummaryVn.Value = _Article.Summary;
                fck_ContentVn.Value = _Article.Content;


            }
            if (!string.IsNullOrEmpty(_Article.ImageThumnail))
            {
                string path = Server.MapPath(RTISettings.NewsPictureFolder);
                if (File.Exists(Path.Combine(path, _Article.ImageThumnail)))
                {
                    img.ImageUrl = RTISettings.NewsPictureFolder + "/" + _Article.ImageThumnail;
                    img.Visible = true;
                    btnDeleteImg.Visible = true;
                }
            }
        }
    }
    protected void SaveData(string _Status)
    {
        Article _Article = new Article();
        if (this.Id != long.MinValue)
        {
            _Article = ArticleService.GetArticle(this.Id);
            _Article.UpdatedDate = DateTime.Now;
            Friso.Entities.User _user = UserService.GetUser(_Article.CreatedBy);
            if (_user != null)
            {
                if (_user.PermissionId != FetConstant.TACGIA)
                {
                    if (UserRight.GetUserId_(HttpContext.Current.User.Identity.Name) != -1)
                        _Article.UpdatedBy = UserRight.GetUserId_(HttpContext.Current.User.Identity.Name);

                }
            }
        }
        else
        {

            _Article.CreatedDate = DateTime.Now;
            if (UserRight.GetUserId_(HttpContext.Current.User.Identity.Name) != -1)
                _Article.CreatedBy = UserRight.GetUserId_(HttpContext.Current.User.Identity.Name);
        }
        _Article.Title = txtName.Text.Trim();
        _Article.SubCatId = long.Parse(ddlSubCat.SelectedValue);
        _Article.Summary = fck_SummaryVn.Value;
        _Article.Content = fck_ContentVn.Value;

       
         
        Criteria cri = new Criteria();
        cri.add("[" + Statu._Table + "].[" + StatuColumns.Code + "]", _Status, Condition.EQUAL, Condition.AND);
        StatuCollection _StatuCollelection = StatuService.GetStatuListByParam("[" + Statu._Table + "].[" + StatuColumns.StatusId + "]", OrderDirection.ASC, cri.Criter);
        if (_StatuCollelection.Count > 0)
        {
            _Article.Status = _StatuCollelection[0].StatusId;
        }
        if (UL_Picture.HasFile)
        {
            string path = Server.MapPath(RTISettings.NewsPictureFolder);
            try
            {
                if (_Article.Image != null && File.Exists(Path.Combine(path, _Article.Image)))
                    File.Delete(Path.Combine(path, _Article.Image));
                if (_Article.ImageThumnail != null && File.Exists(Path.Combine(path, _Article.ImageThumnail)))
                    File.Delete(Path.Combine(path, _Article.ImageThumnail));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            _Article.ImageThumnail = Utilitys.UploadFileImage(UL_Picture.PostedFile, path, 400, 310, RTISettings.MaxFileSize, true);
            _Article.Image = Utilitys.UploadFileImage(UL_Picture.PostedFile, path, RTISettings.NewsWidth, RTISettings.NewsHeight, RTISettings.MaxFileSize, false);
        }
        if (this.Id != long.MinValue)
        {
            ArticleService.UpdateArticle(_Article);
            //upload
            Response.Redirect(prevPage);
        }
        else
        {
            _Article.Order = RTISettings.DefaultOrder;
            _Article.Version = 1;
            ArticleService.CreateArticle(_Article);
            Response.Redirect(prevPage);
        }

    }

    protected void btnDeleteImg_OnClick(object sender, EventArgs e)
    {
        if (this.Id != long.MinValue)
        {
            Article _Article = ArticleService.GetArticle(this.Id);
            if (_Article != null)
            {
                string path = Server.MapPath(RTISettings.NewsPictureFolder);
                try
                {
                    if (_Article.Image != null && File.Exists(Path.Combine(path, _Article.Image)))
                        Utilitys.DelImage(_Article.Image, path);
                    if (_Article.ImageThumnail != null && File.Exists(Path.Combine(path, _Article.ImageThumnail)))
                        Utilitys.DelImage(_Article.ImageThumnail, path);
                    _Article.Image = string.Empty;
                    _Article.ImageThumnail = string.Empty;
                    ArticleService.UpdateArticle(_Article);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }
            }
        }
        Response.Redirect("Article.aspx?id=" + this.Id.ToString() + "&alert=!" + Utilitys.Dictionary["DeleteImg"]);
    }

    protected void btnDangBai_OnClick(object sender, EventArgs e)
    {
        SaveData(FetConstant.Exported);
    }
    protected void ddCategory_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDDlSubCategory(long.Parse(ddlCategory.SelectedValue));
    }







}
